#pragma once
#include "templatevector.h"
#include "Vector2D.h"
#include "Graph.h"
#include "GameEnemy.h"
#include "GamePlayer.h"
#include "mapNode.h"
#define SAFE_RELEASE_TVECTOR_POINTER(x) for(int i = 0; i < x.size(); i++)\
							 {delete x[i];}x.release();

#define SPRITE_SQUARE	64

struct TextureMap
{
	TemplateVector<int> textId;
	void release()
	{
		textId.release();
	}
};
struct map
{
	map():m_player(0),ent_objective(0), ent_exit(0){}
	TemplateVector<GameTexture*> t_walls;
	TemplateVector<GameTexture*> t_floors;
	TemplateVector<char*> tileset;
	TemplateVector<char*> obj;
	char* m_file;
	char* m_title;
	int m_width;
	int m_height;
	TemplateVector<GraphNode<mapNode>*> m_nodes;
	TemplateVector<GraphNode<mapNode>*> m_walls;
	TemplateVector<TextureMap*> tileId;
	List<GameEnemy*> m_enemies;
	GamePlayer* m_player;
	List<V2DF> ent_enemies;
	V2DF ent_player;
	GameObject* ent_objective;
	GameObject* ent_exit;

	TemplateVector<clkObject*> p_walls;

	void posReset()
	{
		for(int i = 0; i < ent_enemies.size(); i++)
		{
			m_enemies[i]->m_body->m_pos = ent_enemies[i];
			m_enemies[i]->resetPath();
		}
		m_player->m_body->m_pos = ent_player;
	}

	void draw()
	{
		int mapy = tileset.size();
		int mapx;
		char tile;
		for (int i = 0; i < mapy; i++)
		{
			mapx = strlen(tileset[i]);
			for (int j = 0; j < mapx; j++)
			{
				tile = tileset[i][j];
				if(tile == '#')
				{
					t_walls[tileId[i]->textId[j]]->m_pos = V2DF((float)j*SPRITE_SQUARE,(float)i*SPRITE_SQUARE);
					t_walls[tileId[i]->textId[j]]->update();
					t_walls[tileId[i]->textId[j]]->draw();
				}
				else if(tile == '.')
				{
					t_floors[tileId[i]->textId[j]]->m_pos = V2DF((float)j*SPRITE_SQUARE,(float)i*SPRITE_SQUARE);
					t_floors[tileId[i]->textId[j]]->update();
					t_floors[tileId[i]->textId[j]]->draw();
				}
			}
		}

	}

	void release()
	{
		SAFE_RELEASE_TVECTOR_POINTER(tileset);
		SAFE_RELEASE_TVECTOR_POINTER(obj);
		for (int i = 0; i < m_nodes.size(); i++)
		{
			m_nodes[i]->release();
		}
		for (int i = 0; i < m_walls.size(); i++)
		{
			m_walls[i]->release();
		}
		for (int i = 0; i < tileId.size(); i++)
		{
			tileId[i]->release();
		}
		SAFE_RELEASE_LIST(m_nodes);
		SAFE_RELEASE_LIST(m_walls);
		SAFE_RELEASE_LIST(tileId);
		ent_enemies.release();
		SAFE_RELEASE_LIST(m_enemies);
		delete m_player;
		SAFE_RELEASE_LIST(t_walls);
		SAFE_RELEASE_LIST(t_floors);
		delete m_file;
		p_walls.release();
	}
};